package com.bdqn.common;

import java.util.List;

/**
 * 分页工具类
 *
 * @author LILIBO
 */
public class Page<T> {

    /**
     * 默认分页页数
     */
    public final static int DEFAULT_PAGE_SIZE = 5;

    // 页大小，每页显示多少条数据
    private int pageSize = DEFAULT_PAGE_SIZE;

    // 当前页码，当前所查看的是第几页
    private int currPageNo = 1;

    // 总页数
    private int totalPageCount = 0;

    // 数据库表中总数据量
    private int totalCount; // select count(*) from tab

    // 分页的数据
    private List<T> list;

    public int getTotalPageCount() {
        return totalPageCount;
    }

    public void setTotalPageCount(int totalPageCount) {
        this.totalPageCount = totalPageCount;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        if (totalCount > 0) {
            this.totalCount = totalCount;
            // 计算总页数
            // 三元表达式：boolean ? 为true时执行 : 为false时执行;
            this.totalPageCount = (totalCount % pageSize == 0) ? (totalCount / pageSize) : (totalCount / pageSize + 1);
        }
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getCurrPageNo() {
        if (totalPageCount == 0) {
            return 0;
        }
        return currPageNo;
    }

    /**
     * 获得查询起始位置
     *
     * @return
     */
    public int getCurrFromNo() {
        currPageNo = currPageNo<=0 ? 1 : currPageNo;
        return (currPageNo - 1) * pageSize;
    }

    public void setCurrPageNo(int currPageNo) {
        if (currPageNo > 0) {
            // 如果当前页比最大页都大，那么赋值最大页给当前页
            if (currPageNo > totalPageCount) {
                currPageNo = totalPageCount;
            }
            this.currPageNo = currPageNo;
        }

    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

}
